<?php
/**
* @version $Id: wookie_subcats.php, v1.0 2005/11/04 00:00:00 Sebastian Unterberg
**/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPrepareContent', '_wookie_subcats' );
$_MAMBOTS->registerFunction( 'onPrepareContent', '_wookie_subcats_detail' );
$_MAMBOTS->registerFunction( 'onPrepareContent', '_wookie_subcats_map' );

//#########################################################################

function is_empty($content_id){
	global $database;
	$database->setQuery("SELECT introtext FROM #__content WHERE id = '".$content_id."'");
	$cts = $database->loadObjectList();
	foreach($cts as $ct) {
		$content = $ct->introtext;
		$content = str_replace(' ','',$content);
		$content = str_replace('&nbsp;','',$content);
		$content = str_replace('<br>','',$content);
		$content = str_replace('<p />','',$content);
		$content = str_replace('<p>&nbsp;</p>','',$content);
		$content = str_replace('<p> </p>','',$content);
		$content = trim($content);
		if(!$content){
			return true;
		} else {
			return false;
		}
	}
}

function get_content_title($content_id){
	global $database;
	$database->setQuery("SELECT title FROM #__content WHERE id = '".$content_id."'");
	$cts = $database->loadObjectList();
	foreach($cts as $ct) {
		return $ct->title;
	}
}

function _wookie_subcats( $published, &$row, &$params, $page=0 ) {
	global $database;
	while(ereg("{subcats:([0-9]*)}",$row->text,$regs)){
		$out = "<ul>";
		$database->setQuery("SELECT * FROM #__menu WHERE menutype = 'mainmenu' AND access <= '".$my->gid."' AND published = '1' AND parent = '".$regs[1]."' ORDER BY ordering");
		$mymenu_rows = $database->loadObjectList();
		foreach($mymenu_rows as $mymenu_row) {
			$prelink =  $mymenu_row->link;
			if ($mymenu_row->type != "url") {
				$prelink .= "&Itemid=".$mymenu_row->id;
			}

			
			$content_title = get_content_title($mymenu_row->componentid);
			if($content_title && ($content_title != $mymenu_row->name)){
				if ($mymenu_row->type == "separator") {
					$out .= "<li title=\"".$content_title."\">".$mymenu_row->name."</li>";
				} else {
					$out .= "<li title=\"".$content_title."\"><a href=\"".sefRelToAbs($prelink)."\">".$mymenu_row->name."</a></li>";
				}
			} else {
				if ($mymenu_row->type == "separator") {
					$out .= "<li>".$mymenu_row->name."</li>";
				} else {
					$out .= "<li><a href=\"".sefRelToAbs($prelink)."\">".$mymenu_row->name."</a></li>";
				}
			}


			$searchtext .= $mymenu_row->name."\n";
		}
		$out .= "</ul>";
	
		$row->text = str_replace("{subcats:".$regs[1]."}",$out,$row->text);
	}
}

function _wookie_subcats_detail( $published, &$row, &$params, $page=0 ) {
	global $database;
	while(ereg("{subcats_detail:([0-9]*)}",$row->text,$regs)){
		$out = "<ul>";
		$database->setQuery("SELECT * FROM #__menu WHERE menutype = 'mainmenu' AND access <= '".$my->gid."' AND published = '1' AND parent = '".$regs[1]."' ORDER BY ordering");
		$mymenu_rows = $database->loadObjectList();
		foreach($mymenu_rows as $mymenu_row) {
			$prelink =  $mymenu_row->link;
			if ($mymenu_row->type != "url") {
				$prelink .= "&Itemid=".$mymenu_row->id;
			}
			
			$content_title = get_content_title($mymenu_row->componentid);
			
			if($content_title && ($content_title != $mymenu_row->name)){
					if ($mymenu_row->type == "separator") {
						$out .= "<li>".$mymenu_row->name." - ".$content_title."</li>";
					} else {
						$out .= "<li><a href=\"".sefRelToAbs($prelink)."\">".$mymenu_row->name."</a> - ".$content_title.$addon."</li>";
					}
			} else {
				if ($mymenu_row->type == "separator") {
					$out .= "<li>".$mymenu_row->name."</li>";
				} else {
					$out .= "<li><a href=\"".sefRelToAbs($prelink)."\">".$mymenu_row->name."</a>".$addon."</li>";
				}
			}
			
			$searchtext .= $mymenu_row->name."\n";
		}
		$out .= "</ul>";
	
		$row->text = str_replace("{subcats_detail:".$regs[1]."}",$out,$row->text);
	}
}

function _wookie_subcats_map( $published, &$row, &$params, $page=0 ) {
	global $database;
	while(ereg("{subcats_map:([0-9]*)}",$row->text,$regs)){
		$out = '<table width="100%" border="0" cellspacing="0" cellpadding="3">';
		$database->setQuery("SELECT * FROM #__menu WHERE menutype = 'mainmenu' AND access <= '".$my->gid."' AND published = '1' AND parent = '".$regs[1]."' ORDER BY ordering");
		$mymenu_rows = $database->loadObjectList();
		foreach($mymenu_rows as $mymenu_row) {
			$prelink =  $mymenu_row->link;
			if ($mymenu_row->type != "url") {
				$prelink .= "&Itemid=".$mymenu_row->id;
			}
			
			if(is_empty($mymenu_row->componentid)){
				$addon = ' <b><font color=red>empty</font></b>';
			} else {
				$addon = false;
			}
			
			$content_title = get_content_title($mymenu_row->componentid);
			
			if($content_title && ($content_title != $mymenu_row->name)){
					if ($mymenu_row->type == "separator") {
						$out .= "<tr><td width='20'>&nbsp;</td><td><img src='./mambots/content/wookie_subcats/bullet.gif' hspace='6'>".$mymenu_row->name." - ".$content_title."</td></tr>";
					} else {
						$out .= "<tr><td width='20'>&nbsp;</td><td><img src='./mambots/content/wookie_subcats/bullet.gif' hspace='6'><a href=\"".sefRelToAbs($prelink)."\">".$mymenu_row->name."</a> - ".$content_title.$addon."{subcats_map:".$mymenu_row->id."}</td></tr>";
					}
			} else {
				if ($mymenu_row->type == "separator") {
					$out .= "<tr><td width='20'>&nbsp;</td><td><img src='./mambots/content/wookie_subcats/bullet.gif' hspace='6'>".$mymenu_row->name."</td></tr>";
				} else {
					$out .= "<tr><td width='20'>&nbsp;</td><td><img src='./mambots/content/wookie_subcats/bullet.gif' hspace='6'><a href=\"".sefRelToAbs($prelink)."\">".$mymenu_row->name."</a>".$addon."{subcats_map:".$mymenu_row->id."}</td></tr>";
				}
			}
			
			$searchtext .= $mymenu_row->name."\n";
		}
		$out .= "</table>";
	
		$row->text = str_replace("{subcats_map:".$regs[1]."}",$out,$row->text);
	}
}

?>